package top.zywork.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.zywork.annotation.SysLog;
import top.zywork.common.BeanUtils;
import top.zywork.common.BindingResultUtils;
import top.zywork.common.StringUtils;
import top.zywork.dos.CourierExtractingDO;
import top.zywork.dto.PagerDTO;
import top.zywork.dto.CourierExtractingDTO;
import top.zywork.query.CourierExtractingQuery;
import top.zywork.service.CourierExtractingService;
import top.zywork.service.SchoolService;
import top.zywork.vo.ResponseStatusVO;
import top.zywork.vo.PagerVO;
import top.zywork.vo.CourierExtractingVO;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * CourierExtractingController控制器类<br/>
 *
 * 创建于2020-10-27<br/>
 *
 * @author http://ljyanimo.com 刘金泳
 * @version 1.0
 */
@RestController
@RequestMapping("/courier-extracting")
@Slf4j
public class CourierExtractingController extends BaseController {

    private CourierExtractingService courierExtractingService;

    @Autowired
    private SchoolService schoolService;

    @PostMapping("admin/save")
    @SysLog(description = "后台保存CourierExtractingVO")
    public ResponseStatusVO save(@RequestBody @Validated CourierExtractingVO courierExtractingVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        courierExtractingService.save(BeanUtils.copy(courierExtractingVO, CourierExtractingDTO.class));
        return ResponseStatusVO.ok("添加成功", null);
    }

    @PostMapping("admin/batch-save")
    @SysLog(description = "后台批量保存CourierExtractingVO")
    public ResponseStatusVO saveBatch(@RequestBody @Validated List<CourierExtractingVO> courierExtractingVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        courierExtractingService.saveBatch(BeanUtils.copyListObj(courierExtractingVOList, CourierExtractingDTO.class));
        return ResponseStatusVO.ok("批量添加成功", null);
    }

    @GetMapping("admin/remove/{id}")
    @SysLog(description = "后台删除CourierExtractingVO")
    public ResponseStatusVO removeById(@PathVariable("id") Long id) {
        courierExtractingService.removeById(id);
        return ResponseStatusVO.ok("删除成功", null);
    }

    @PostMapping("admin/batch-remove")
    @SysLog(description = "后台批量删除CourierExtractingVO")
    public ResponseStatusVO removeByIds(@RequestBody String[] ids) {
        courierExtractingService.removeByIds(StringUtils.strArrayToLongArray(ids));
        return ResponseStatusVO.ok("批量删除成功", null);
    }

    @PostMapping("admin/update")
    @SysLog(description = "后台更新CourierExtractingVO")
    public ResponseStatusVO update(@RequestBody @Validated CourierExtractingVO courierExtractingVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        CourierExtractingDTO copy = BeanUtils.copy(courierExtractingVO, CourierExtractingDTO.class);
        int updateRows = courierExtractingService.update(copy);
        if (updateRows == 1) {
            return ResponseStatusVO.ok("更新成功", null);
        } else {
            return ResponseStatusVO.dataError("数据版本号有问题，在此更新前数据已被更新", null);
        }
    }

    @PostMapping("admin/batch-update")
    @SysLog(description = "后台批量更新CourierExtractingVO")
    public ResponseStatusVO updateBatch(@RequestBody @Validated List<CourierExtractingVO> courierExtractingVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        courierExtractingService.updateBatch(BeanUtils.copyListObj(courierExtractingVOList, CourierExtractingDTO.class));
        return ResponseStatusVO.ok("批量更新成功", null);
    }

    @PostMapping("admin/active")
    @SysLog(description = "后台激活或冻结CourierExtractingVO")
    public ResponseStatusVO active(@RequestBody CourierExtractingVO courierExtractingVO) {
        courierExtractingService.update(BeanUtils.copy(courierExtractingVO, CourierExtractingDTO.class));
        return ResponseStatusVO.ok("激活或冻结成功", null);
    }

    @PostMapping("admin/batch-active")
    @SysLog(description = "后台批量激活或冻结CourierExtractingVO")
    public ResponseStatusVO activeBatch(@RequestBody @Validated List<CourierExtractingVO> courierExtractingVOList) {
        courierExtractingService.updateBatch(BeanUtils.copyListObj(courierExtractingVOList, CourierExtractingDTO.class));
        return ResponseStatusVO.ok("批量激活或冻结成功", null);
    }

    @GetMapping("admin/one/{id}")
    public ResponseStatusVO getById(@PathVariable("id") Long id) {
        CourierExtractingVO courierExtractingVO = new CourierExtractingVO();
        Object obj = courierExtractingService.getById(id);
        if (obj != null) {
            courierExtractingVO = BeanUtils.copy(obj, CourierExtractingVO.class);
        }
        return ResponseStatusVO.ok("查询成功", courierExtractingVO);
    }

    @GetMapping("admin/all")
    public ResponseStatusVO listAll() {
        PagerDTO pagerDTO = courierExtractingService.listAll();
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), CourierExtractingVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/all-cond")
    public ResponseStatusVO listAllByCondition(@RequestBody CourierExtractingQuery courierExtractingQuery) {
        PagerDTO pagerDTO = courierExtractingService.listAllByCondition(courierExtractingQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), CourierExtractingVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/pager-cond")
    public ResponseStatusVO listPageByCondition(@RequestBody CourierExtractingQuery courierExtractingQuery) {
        PagerDTO pagerDTO = courierExtractingService.listPageByCondition(courierExtractingQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);

        List<Object> list;

        list = pagerDTO.getRows().stream().map(e->{
            CourierExtractingDTO courierExtractingDTO = (CourierExtractingDTO)e;
            CourierExtractingVO courierExtractingVO = new CourierExtractingVO();
            BeanUtils.copy(e,courierExtractingVO);
            courierExtractingVO.setSchoolName(schoolService.getById(courierExtractingDTO.getSchoolId()).getSchool());
            return courierExtractingVO;
        }).collect(Collectors.toList());
        pagerVO.setRows(list);
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @Autowired
    public void setCourierExtractingService(CourierExtractingService courierExtractingService) {
        this.courierExtractingService = courierExtractingService;
    }
}
